<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增资讯')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ">
    <form id="form-detail-add">
        <input id="userId" name="userId" type="hidden" th:value="${userId}">
        <input id="filePath" name="filePath" type="hidden" th:value="${filePath}">
        <input id="flag"  type="hidden" th:value="${data}">
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <div class="col-sm-12">
                        <input id="userFile" name="userFile" class="form-control" type="file"  accept="application/msword">
                        <div id="showFile" style="display:none" >
                            <input id="fileName" name="fileName" type="text" th:value="${fileName}" class="form-control" readonly>
                            <iframe id="ifr" width='100%' height='900px;' frameborder='1'>
                            </iframe>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script type="text/javascript">
    var prefix = ctx + "system/user"
    $("#form-news-add").validate({
        focusCleanup: true
    });

    $(function () {

        var flag = $("#flag").val();
        if (flag == 1) {
            document.getElementById("showFile").style.display = "none";
        } else {
            document.getElementById("showFile").style.display = "block";
            $("#ifr").attr("src",'https://view.officeapps.live.com/op/view.aspx?src='+$("#filePath").val()) ;
        }
    });


    function submitHandler() {
        if ($.validate.form()) {
            uploadFile();
            //  $.operate.save(prefix + "/add", $('#form-detail-add').serialize());
        }
    }

    //提交
    function uploadFile() {
        var formData = new FormData();
        if ($('#userFile')[0].files[0] == null) {
            $.modal.alertWarning("请先选择文件路径");
            return false;
        }
        console.log($('#userFile')[0].files[0]);
        formData.append('userId', $("#userId").val());
        formData.append('file', $('#userFile')[0].files[0]);
        $.ajax({
            url: prefix + "/detail",
            type: "post",
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });
    }

</script>
</body>
</html>